﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tyng.Data
{
    /// <summary>
    /// Generic data record based on an array of values in field order.
    /// </summary>
    public class ObjectArrayDataRecord : FieldDataRecord
    {
        object[] _values;

        /// <summary>
        /// Constructor that takes values in string format and converts them using field objects.
        /// </summary>
        /// <param name="values"></param>
        /// <param name="fields"></param>
        public ObjectArrayDataRecord(string[] values, Field[] fields) : base(fields)
        {
            _values = new object[fields.Length];

            for (int i = 0; i < fields.Length; i++)
            {
                if (values.Length > i)
                    _values[i] = fields[i].ConvertFromString(values[i]);
                else
                {
                    //TODO: should it throw if a column is missing??
                    _values[i] = null;
                }
            }

        }

        /// <summary>
        /// Constructor that takes raw object data.
        /// </summary>
        /// <param name="values"></param>
        /// <param name="fields"></param>
        public ObjectArrayDataRecord(object[] values, Field[] fields)
            : base(fields)
        {
            _values = values;
        }

        /// <summary>
        /// Get the value from the object array.
        /// </summary>
        /// <param name="i">Array / field index</param>
        /// <returns></returns>
        protected override object GetValue(int i)
        {
            return _values[i];
        }
    }
}
